/**
 * 
 */
package com.gs.gms.model.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

/**
 * @author Sabuj Das | sabuj.das@gmail.com
 *
 */
@Entity
@Table(name="GMS_USER_SESSION",
	uniqueConstraints={
		@UniqueConstraint(columnNames={"SESSION_ID", "USER_ID"})
	})
public class UserSession {

	@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="USER_SESSION_ID", unique=true, nullable=false, updatable=false)
	private Long userSessionId;
	
	@Column(name="SESSION_ID", unique=true, nullable=false, length=228)
	private String sessionId;
	
	@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="USER_ID", unique=true, nullable=false)
	private User user;
	
	@Column(name="SIGN_IN_TIME", nullable=false, length=19)
	@Temporal(TemporalType.TIMESTAMP)
	private Date signInTime;

	@Column(name="LAST_ACTIVE_AT", nullable=true, length=19)
	@Temporal(TemporalType.TIMESTAMP)
	private Date lastActiveAt;
	
	@Column(name="SIGN_OUT_TIME", nullable=true, length=19)
	@Temporal(TemporalType.TIMESTAMP)
	private Date signOutTime;
	
	@Column(name="IS_ACTIVE", nullable = false, columnDefinition = " TINYINT(1) ")
	private boolean active;
	
	/**
	 * 
	 */
	public UserSession() {
		// TODO Auto-generated constructor stub
	}

	public Long getUserSessionId() {
		return userSessionId;
	}

	public void setUserSessionId(Long userSessionId) {
		this.userSessionId = userSessionId;
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Date getSignInTime() {
		return signInTime;
	}

	public void setSignInTime(Date signInTime) {
		this.signInTime = signInTime;
	}

	public Date getLastActiveAt() {
		return lastActiveAt;
	}

	public void setLastActiveAt(Date lastActiveAt) {
		this.lastActiveAt = lastActiveAt;
	}

	public Date getSignOutTime() {
		return signOutTime;
	}

	public void setSignOutTime(Date signOutTime) {
		this.signOutTime = signOutTime;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((sessionId == null) ? 0 : sessionId.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof UserSession)) {
			return false;
		}
		UserSession other = (UserSession) obj;
		if (sessionId == null) {
			if (other.sessionId != null) {
				return false;
			}
		} else if (!sessionId.equals(other.sessionId)) {
			return false;
		}
		if (user == null) {
			if (other.user != null) {
				return false;
			}
		} else if (!user.equals(other.user)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return String
				.format("UserSession [userSessionId=%s, sessionId=%s, user=%s, active=%s]",
						userSessionId, sessionId, user, active);
	}
	
	
}
